

clear
set more 1

capture log using "log/decomp.log", replace
*this file provides variance decomp of currency choice


*exporting

use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop date *I
	rename country cty_code
	drop if missing(valueX)
	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueX value
	
	
	*deal with multiple currencies (4.8% duplicates)
	gen cat=currency=="EUR"
	collapse (sum) value, by(vat cncode cty_code year month cat)

	egen totvalue=sum(value), by(vat cncode cty_code year month)

	gen value_EUR=value if cat==1
	gen sh_EUR=value_EUR/totvalue
	replace  sh_EUR=0 if cat==0
	
	*only keep euro obs when multiple and report value share
	duplicates tag vat cncode cty_code year month, gen(tag)
	drop if tag==1 & cat==0
	gen y=1 - sh_EUR	
	
*fixed effects		
	gen hs4=substr(cncode,1,4)
	egen cty_num=group(cty_code)
	egen c_hs4=group(cty_code hs4)
	egen c_hs8=group(cty_code cncode)
	egen f_c=group(vat cty_code)
	egen f_hs8=group(vat cncode)

*weighted
*col1: firm
	eststo: reghdfe y [w=value], absorb(vat)
*col2: firm X country 	
	eststo: reghdfe y [w=value], absorb(f_c)
*col3: country	
	eststo: reghdfe y [w=value], absorb(cty_num)
*col4: HS4 industry	
	eststo: reghdfe y [w=value], absorb(hs4)
*col5: HS4 X country
	eststo: reghdfe y [w=value], absorb(c_hs4)
*col6: HS8 X country
	eststo: reghdfe y [w=value], absorb(c_hs8)
*col7: HS4 X country plus firm X country	
	eststo: reghdfe y [w=value], absorb(f_c c_hs4)
*col8: 	HS8 X country plus firm X country	
	eststo: reghdfe y [w=value], absorb(f_c c_hs8)
	
*unweighted
*col1: firm
	eststo: reghdfe y, absorb(vat)
*col2: firm X country 	
	eststo: reghdfe y, absorb(f_c)
*col3: country	
	eststo: reghdfe y, absorb(cty_num)
*col4: HS4 industry	
	eststo: reghdfe y, absorb(hs4)
*col5: HS4 X country
	eststo: reghdfe y, absorb(c_hs4)
*col6: HS8 X country
	eststo: reghdfe y, absorb(c_hs8)
*col7: HS4 X country plus firm X country	
	eststo: reghdfe y, absorb(f_c c_hs4)
*col8: HS4 X country plus firm X country	
	eststo: reghdfe y, absorb(f_c c_hs8)	
	
esttab using "../results/sumstats/TableA2_decomp_exports.csv", se keep( ) b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) order()  compress replace
eststo clear


	
*******************************
*importing
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop date *X
	rename country cty_code
	drop if missing(valueI)
	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueI value
	
	*deal with multiple currencies (10% duplicates)
	gen cat=currency=="EUR"
	collapse (sum) value, by(vat cncode cty_code year month cat)

	egen totvalue=sum(value), by(vat cncode cty_code year month)

	gen value_EUR=value if cat==1
	gen sh_EUR=value_EUR/totvalue
	replace  sh_EUR=0 if cat==0

	*only keep euro obs when multiple and report value share
	duplicates tag vat cncode cty_code year month, gen(tag)
	drop if tag==1 & cat==0
	
	gen y=1 - sh_EUR
	
*fixed effects		
	gen hs4=substr(cncode,1,4)
	egen cty_num=group(cty_code)
	egen c_hs4=group(cty_code hs4)
	egen c_hs8=group(cty_code cncode)
	egen f_c=group(vat cty_code)
	egen f_hs8=group(vat cncode)

*weighted
*col1: firm
	eststo: reghdfe y [w=value], absorb(vat)
*col2: firm X country 	
	eststo: reghdfe y [w=value], absorb(f_c)
*col3: country	
	eststo: reghdfe y [w=value], absorb(cty_num)
*col4: HS4 industry	
	eststo: reghdfe y [w=value], absorb(hs4)
*col5: HS4 X country
	eststo: reghdfe y [w=value], absorb(c_hs4)
*col6: HS8 X country
	eststo: reghdfe y [w=value], absorb(c_hs8)
*col7: HS4 X country plus firm X country	
	eststo: reghdfe y [w=value], absorb(f_c c_hs4)
*col8: HS8 X country plus firm X country	
	eststo: reghdfe y [w=value], absorb(f_c c_hs8)
	

*unweighted
*col1: firm
	eststo: reghdfe y, absorb(vat)
*col2: firm X country 	
	eststo: reghdfe y, absorb(f_c)
*col3: country	
	eststo: reghdfe y, absorb(cty_num)
*col4: HS4 industry	
	eststo: reghdfe y, absorb(hs4)
*col5: HS4 X country
	eststo: reghdfe y, absorb(c_hs4)
*col6: HS8 X country
	eststo: reghdfe y, absorb(c_hs8)
*col7: HS4 X country plus firm X country	
	eststo: reghdfe y, absorb(f_c c_hs4)
*col8: HS8 X country plus firm X country	
	eststo: reghdfe y, absorb(f_c c_hs8)

	
esttab using "../results/sumstats/ATable_decomp_imports.csv", se keep( ) b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) order()  compress replace
eststo clear
	
log close


	
	
	
	
	
	
	
	
	